


<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>TorchScript Language Reference &mdash; PyTorch master documentation</title>
  

  
  
  
  
    <link rel="canonical" href="https://pytorch.org/docs/stable/jit_language_reference.html"/>
  

  

  
  
    

  

  <link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
  <!-- <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.10.0-beta/dist/katex.min.css" type="text/css" />
  <link rel="stylesheet" href="_static/css/jit.css" type="text/css" />
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css" type="text/css" />
  <link rel="stylesheet" href="_static/katex-math.css" type="text/css" />
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="torch.nn.init" href="nn.init.html" />
    <link rel="prev" title="TorchScript Builtins" href="jit_builtin_functions.html" /> 

  
  <script src="_static/js/modernizr.min.js"></script>

  <!-- Preload the theme fonts -->

<link rel="preload" href="_static/fonts/FreightSans/freight-sans-book.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/FreightSans/freight-sans-medium.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/IBMPlexMono/IBMPlexMono-Medium.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/FreightSans/freight-sans-bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/FreightSans/freight-sans-medium-italic.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/IBMPlexMono/IBMPlexMono-SemiBold.woff2" as="font" type="font/woff2" crossorigin="anonymous">

<!-- Preload the katex fonts -->

<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Math-Italic.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Main-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Main-Bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size1-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size4-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size2-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size3-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Caligraphic-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
</head>

<div class="container-fluid header-holder tutorials-header" id="header-holder">
  <div class="container">
    <div class="header-container">
      <a class="header-logo" href="https://pytorch.org/" aria-label="PyTorch"></a>

      <div class="main-menu">
        <ul>
          <li>
            <a href="https://pytorch.org/get-started">Get Started</a>
          </li>

          <li>
            <div class="ecosystem-dropdown">
              <a id="dropdownMenuButton" data-toggle="ecosystem-dropdown">
                Ecosystem
              </a>
              <div class="ecosystem-dropdown-menu">
                <a class="nav-dropdown-item" href="https://pytorch.org/hub"">
                  <span class=dropdown-title>Models (Beta)</span>
                  <p>Discover, publish, and reuse pre-trained models</p>
                </a>
                <a class="nav-dropdown-item" href="https://pytorch.org/ecosystem">
                  <span class=dropdown-title>Tools & Libraries</span>
                  <p>Explore the ecosystem of tools and libraries</p>
                </a>
              </div>
            </div>
          </li>

          <li>
            <a href="https://pytorch.org/mobile">Mobile</a>
          </li>

          <li>
            <a href="https://pytorch.org/blog/">Blog</a>
          </li>

          <li>
            <a href="https://pytorch.org/tutorials">Tutorials</a>
          </li>

          <li class="active">
            <a href="https://pytorch.org/docs/stable/index.html">Docs</a>
          </li>

          <li>
            <div class="resources-dropdown">
              <a id="resourcesDropdownButton" data-toggle="resources-dropdown">
                Resources
              </a>
              <div class="resources-dropdown-menu">
                <a class="nav-dropdown-item" href="https://pytorch.org/resources"">
                  <span class=dropdown-title>Developer Resources</span>
                  <p>Find resources and get questions answered</p>
                </a>
                <a class="nav-dropdown-item" href="https://pytorch.org/features">
                  <span class=dropdown-title>About</span>
                  <p>Learn about PyTorch’s features and capabilities</p>
                </a>
              </div>
            </div>
          </li>

          <li>
            <a href="https://github.com/pytorch/pytorch">Github</a>
          </li>
        </ul>
      </div>

      <a class="main-menu-open-button" href="#" data-behavior="open-mobile-menu"></a>
    </div>

  </div>
</div>


<body class="pytorch-body">

   

    

    <div class="table-of-contents-link-wrapper">
      <span>Table of Contents</span>
      <a href="#" class="toggle-table-of-contents" data-behavior="toggle-table-of-contents"></a>
    </div>

    <nav data-toggle="wy-nav-shift" class="pytorch-left-menu" id="pytorch-left-menu">
      <div class="pytorch-side-scroll">
        <div class="pytorch-menu pytorch-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          <div class="pytorch-left-menu-search">
            

            
              
              
                <div class="version">
                  master (1.5.0 )
                </div>
              
            

            


  


<div role="search">
  <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
    <input type="text" name="q" placeholder="Search Docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

            
          </div>

          
<div>
  <a style="color:#F05732" href="https://pytorch.org/docs/stable/jit_language_reference.html">
    You are viewing unstable developer preview docs.
    Click here to view docs for latest stable release.
  </a>
</div>

            
            
              
            
            
              <p class="caption"><span class="caption-text">Notes</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="notes/amp_examples.html">Automatic Mixed Precision examples</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/autograd.html">Autograd mechanics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/broadcasting.html">Broadcasting semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/cpu_threading_torchscript_inference.html">CPU threading and TorchScript inference</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/cuda.html">CUDA semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/ddp.html">Distributed Data Parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/extending.html">Extending PyTorch</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/faq.html">Frequently Asked Questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/large_scale_deployments.html">Features for large-scale deployments</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/multiprocessing.html">Multiprocessing best practices</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/randomness.html">Reproducibility</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/serialization.html">Serialization semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/windows.html">Windows FAQ</a></li>
</ul>
<p class="caption"><span class="caption-text">Language Bindings</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/cppdocs/">C++ API</a></li>
<li class="toctree-l1"><a class="reference internal" href="packages.html">Javadoc</a></li>
</ul>
<p class="caption"><span class="caption-text">Python API</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="torch.html">torch</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.html">torch.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.functional.html">torch.nn.functional</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensors.html">torch.Tensor</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensor_attributes.html">Tensor Attributes</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensor_view.html">Tensor Views</a></li>
<li class="toctree-l1"><a class="reference internal" href="autograd.html">torch.autograd</a></li>
<li class="toctree-l1"><a class="reference internal" href="cuda.html">torch.cuda</a></li>
<li class="toctree-l1"><a class="reference internal" href="amp.html">torch.cuda.amp</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributed.html">torch.distributed</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributions.html">torch.distributions</a></li>
<li class="toctree-l1"><a class="reference internal" href="hub.html">torch.hub</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="jit.html">torch.jit</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.init.html">torch.nn.init</a></li>
<li class="toctree-l1"><a class="reference internal" href="onnx.html">torch.onnx</a></li>
<li class="toctree-l1"><a class="reference internal" href="optim.html">torch.optim</a></li>
<li class="toctree-l1"><a class="reference internal" href="quantization.html">Quantization</a></li>
<li class="toctree-l1"><a class="reference internal" href="rpc/index.html">Distributed RPC Framework</a></li>
<li class="toctree-l1"><a class="reference internal" href="random.html">torch.random</a></li>
<li class="toctree-l1"><a class="reference internal" href="sparse.html">torch.sparse</a></li>
<li class="toctree-l1"><a class="reference internal" href="storage.html">torch.Storage</a></li>
<li class="toctree-l1"><a class="reference internal" href="bottleneck.html">torch.utils.bottleneck</a></li>
<li class="toctree-l1"><a class="reference internal" href="checkpoint.html">torch.utils.checkpoint</a></li>
<li class="toctree-l1"><a class="reference internal" href="cpp_extension.html">torch.utils.cpp_extension</a></li>
<li class="toctree-l1"><a class="reference internal" href="data.html">torch.utils.data</a></li>
<li class="toctree-l1"><a class="reference internal" href="dlpack.html">torch.utils.dlpack</a></li>
<li class="toctree-l1"><a class="reference internal" href="model_zoo.html">torch.utils.model_zoo</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensorboard.html">torch.utils.tensorboard</a></li>
<li class="toctree-l1"><a class="reference internal" href="type_info.html">Type Info</a></li>
<li class="toctree-l1"><a class="reference internal" href="named_tensor.html">Named Tensors</a></li>
<li class="toctree-l1"><a class="reference internal" href="name_inference.html">Named Tensors operator coverage</a></li>
<li class="toctree-l1"><a class="reference internal" href="__config__.html">torch.__config__</a></li>
</ul>
<p class="caption"><span class="caption-text">Libraries</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/audio">torchaudio</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/text">torchtext</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/elastic/">TorchElastic</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/serve">TorchServe</a></li>
<li class="toctree-l1"><a class="reference external" href="http://pytorch.org/xla/">PyTorch on XLA Devices</a></li>
</ul>
<p class="caption"><span class="caption-text">Community</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="community/contribution_guide.html">PyTorch Contribution Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="community/governance.html">PyTorch Governance</a></li>
<li class="toctree-l1"><a class="reference internal" href="community/persons_of_interest.html">PyTorch Governance | Persons of Interest</a></li>
</ul>

            
          

        </div>
      </div>
    </nav>

    <div class="pytorch-container">
      <div class="pytorch-page-level-bar" id="pytorch-page-level-bar">
        <div class="pytorch-breadcrumbs-wrapper">
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="pytorch-breadcrumbs">
    
      <li>
        <a href="index.html">
          
            Docs
          
        </a> &gt;
      </li>

        
          <li><a href="jit.html">TorchScript</a> &gt;</li>
        
      <li>TorchScript Language Reference</li>
    
    
      <li class="pytorch-breadcrumbs-aside">
        
            
            <a href="_sources/jit_language_reference.rst.txt" rel="nofollow"><img src="_static/images/view-page-source-icon.svg"></a>
          
        
      </li>
    
  </ul>

  
</div>
        </div>

        <div class="pytorch-shortcuts-wrapper" id="pytorch-shortcuts-wrapper">
          Shortcuts
        </div>
      </div>

      <section data-toggle="wy-nav-shift" id="pytorch-content-wrap" class="pytorch-content-wrap">
        <div class="pytorch-content-left">

        
          
          <div class="rst-content">
          
            <div role="main" class="main-content" itemscope="itemscope" itemtype="http://schema.org/Article">
             <article itemprop="articleBody" id="pytorch-article" class="pytorch-article">
              
  <div class="contents local topic" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#torchscript-language-reference" id="id5">TorchScript Language Reference</a></p>
<ul>
<li><p><a class="reference internal" href="#supported-type" id="id6">Types</a></p></li>
<li><p><a class="reference internal" href="#expressions" id="id7">Expressions</a></p></li>
<li><p><a class="reference internal" href="#statements" id="id8">Statements</a></p></li>
<li><p><a class="reference internal" href="#variable-resolution" id="id9">Variable Resolution</a></p></li>
<li><p><a class="reference internal" href="#use-of-python-values" id="id10">Use of Python Values</a></p></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="torchscript-language-reference">
<span id="language-reference"></span><h1><a class="toc-backref" href="#id5">TorchScript Language Reference</a><a class="headerlink" href="#torchscript-language-reference" title="Permalink to this headline">¶</a></h1>
<p>TorchScript is a statically typed subset of Python that can either be written directly (using
the <a class="reference internal" href="jit.html#torch.jit.script" title="torch.jit.script"><code class="xref py py-func docutils literal notranslate"><span class="pre">&#64;torch.jit.script</span></code></a> decorator) or generated automatically from Python code via
tracing. When using tracing, code is automatically converted into this subset of
Python by recording only the actual operators on tensors and simply executing and
discarding the other surrounding Python code.</p>
<p>When writing TorchScript directly using <code class="docutils literal notranslate"><span class="pre">&#64;torch.jit.script</span></code> decorator, the programmer must
only use the subset of Python supported in TorchScript. This section documents
what is supported in TorchScript as if it were a language reference for a stand
alone language. Any features of Python not mentioned in this reference are not
part of TorchScript. See <a href="#id11"><span class="problematic" id="id12">`Builtin Functions`_</span></a> for a complete reference of available
Pytorch tensor methods, modules, and functions.</p>
<p>As a subset of Python, any valid TorchScript function is also a valid Python
function. This makes it possible to <a href="#id13"><span class="problematic" id="id14">`disable TorchScript`_</span></a> and debug the
function using standard Python tools like <code class="docutils literal notranslate"><span class="pre">pdb</span></code>. The reverse is not true: there
are many valid Python programs that are not valid TorchScript programs.
Instead, TorchScript focuses specifically on the features of Python that are
needed to represent neural network models in PyTorch.</p>
<div class="section" id="supported-type">
<span id="types"></span><span id="id1"></span><h2><a class="toc-backref" href="#id6">Types</a><a class="headerlink" href="#supported-type" title="Permalink to this headline">¶</a></h2>
<p>The largest difference between TorchScript and the full Python language is that
TorchScript only supports a small set of types that are needed to express neural
net models. In particular, TorchScript supports:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Type</p></th>
<th class="head"><p>Description</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">Tensor</span></code></p></td>
<td><p>A PyTorch tensor of any dtype, dimension, or backend</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">Tuple[T0,</span> <span class="pre">T1,</span> <span class="pre">...]</span></code></p></td>
<td><p>A tuple containing subtypes <code class="docutils literal notranslate"><span class="pre">T0</span></code>, <code class="docutils literal notranslate"><span class="pre">T1</span></code>, etc. (e.g. <code class="docutils literal notranslate"><span class="pre">Tuple[Tensor,</span> <span class="pre">Tensor]</span></code>)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">bool</span></code></p></td>
<td><p>A boolean value</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">int</span></code></p></td>
<td><p>A scalar integer</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">float</span></code></p></td>
<td><p>A scalar floating point number</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">str</span></code></p></td>
<td><p>A string</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">List[T]</span></code></p></td>
<td><p>A list of which all members are type <code class="docutils literal notranslate"><span class="pre">T</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">Optional[T]</span></code></p></td>
<td><p>A value which is either None or type <code class="docutils literal notranslate"><span class="pre">T</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">Dict[K,</span> <span class="pre">V]</span></code></p></td>
<td><p>A dict with key type <code class="docutils literal notranslate"><span class="pre">K</span></code> and value type <code class="docutils literal notranslate"><span class="pre">V</span></code>. Only <code class="docutils literal notranslate"><span class="pre">str</span></code>, <code class="docutils literal notranslate"><span class="pre">int</span></code>, and <code class="docutils literal notranslate"><span class="pre">float</span></code> are allowed as key types.</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">T</span></code></p></td>
<td><p>A <a class="reference internal" href="#torchscript-class">TorchScript Class</a></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">NamedTuple[T0,</span> <span class="pre">T1,</span> <span class="pre">...]</span></code></p></td>
<td><p>A <a class="reference external" href="https://docs.python.org/3/library/collections.html#collections.namedtuple" title="(in Python v3.8)"><code class="xref py py-func docutils literal notranslate"><span class="pre">collections.namedtuple</span></code></a> tuple type</p></td>
</tr>
</tbody>
</table>
<p>Unlike Python, each variable in TorchScript function must have a single static type.
This makes it easier to optimize TorchScript functions.</p>
<p>Example (a type mismatch)</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>

<span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span>
<span class="k">def</span> <span class="nf">an_error</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">x</span><span class="p">:</span>
        <span class="n">r</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">r</span> <span class="o">=</span> <span class="mi">4</span>
    <span class="k">return</span> <span class="n">r</span>
</pre></div>
</div>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Traceback (most recent call last):
  ...
RuntimeError: ...

Type mismatch: r is set to type Tensor in the true branch and type int in the false branch:
@torch.jit.script
def an_error(x):
    if x:
    ~~~~~...  &lt;--- HERE
        r = torch.rand(1)
    else:
and was used here:
    else:
        r = 4
    return r
           ~ &lt;--- HERE
...
</pre></div>
</div>
<div class="section" id="unsupported-typing-constructs">
<h3>Unsupported Typing Constructs<a class="headerlink" href="#unsupported-typing-constructs" title="Permalink to this headline">¶</a></h3>
<p>TorchScript does not support all features and types of the <a class="reference external" href="https://docs.python.org/3/library/typing.html#module-typing" title="(in Python v3.8)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">typing</span></code></a> module. Some of these
are more fundamental things that are unlikely to be added in the future while others
may be added if there is enough user demand to make it a priority.</p>
<p>These types and features from the <a class="reference external" href="https://docs.python.org/3/library/typing.html#module-typing" title="(in Python v3.8)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">typing</span></code></a> module are unavailble in TorchScript.</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Item</p></th>
<th class="head"><p>Description</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Any" title="(in Python v3.8)"><code class="xref any docutils literal notranslate"><span class="pre">typing.Any</span></code></a></p></td>
<td><p><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Any" title="(in Python v3.8)"><code class="xref any docutils literal notranslate"><span class="pre">typing.Any</span></code></a> is currently in development but not yet released</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.NoReturn" title="(in Python v3.8)"><code class="xref any docutils literal notranslate"><span class="pre">typing.NoReturn</span></code></a></p></td>
<td><p>Not implemented</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Union" title="(in Python v3.8)"><code class="xref any docutils literal notranslate"><span class="pre">typing.Union</span></code></a></p></td>
<td><p>Unlikely to be implemented (however <a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Optional" title="(in Python v3.8)"><code class="xref any docutils literal notranslate"><span class="pre">typing.Optional</span></code></a> is supported)</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Callable" title="(in Python v3.8)"><code class="xref any docutils literal notranslate"><span class="pre">typing.Callable</span></code></a></p></td>
<td><p>Not implemented</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Literal" title="(in Python v3.8)"><code class="xref any docutils literal notranslate"><span class="pre">typing.Literal</span></code></a></p></td>
<td><p>Not implemented</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.ClassVar" title="(in Python v3.8)"><code class="xref any docutils literal notranslate"><span class="pre">typing.ClassVar</span></code></a></p></td>
<td><p>Not implemented</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Final" title="(in Python v3.8)"><code class="xref any docutils literal notranslate"><span class="pre">typing.Final</span></code></a></p></td>
<td><p>This is supported for <a class="reference internal" href="#module-attributes"><span class="std std-ref">module attributes</span></a> class attribute annotations but not for functions</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.AnyStr" title="(in Python v3.8)"><code class="xref any docutils literal notranslate"><span class="pre">typing.AnyStr</span></code></a></p></td>
<td><p>TorchScript does not support <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#bytes" title="(in Python v3.8)"><code class="xref any docutils literal notranslate"><span class="pre">bytes</span></code></a> so this type is not used</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.overload" title="(in Python v3.8)"><code class="xref any docutils literal notranslate"><span class="pre">typing.overload</span></code></a></p></td>
<td><p><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.overload" title="(in Python v3.8)"><code class="xref any docutils literal notranslate"><span class="pre">typing.overload</span></code></a> is currently in development but not yet released</p></td>
</tr>
<tr class="row-odd"><td><p>Type aliases</p></td>
<td><p>Not implemented</p></td>
</tr>
<tr class="row-even"><td><p>Nominal vs structural subtyping</p></td>
<td><p>Nominal typing is in development, but structural typing is not</p></td>
</tr>
<tr class="row-odd"><td><p>NewType</p></td>
<td><p>Unlikely to be implemented</p></td>
</tr>
<tr class="row-even"><td><p>Generics</p></td>
<td><p>Unlikely to be implemented</p></td>
</tr>
</tbody>
</table>
<p>Any other functionality from the <a class="reference external" href="https://docs.python.org/3/library/typing.html#module-typing" title="(in Python v3.8)"><code class="xref any docutils literal notranslate"><span class="pre">typing</span></code></a> module not explitily listed in this documentation is unsupported.</p>
</div>
<div class="section" id="default-types">
<h3>Default Types<a class="headerlink" href="#default-types" title="Permalink to this headline">¶</a></h3>
<p>By default, all parameters to a TorchScript function are assumed to be Tensor.
To specify that an argument to a TorchScript function is another type, it is possible to use
MyPy-style type annotations using the types listed above.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>

<span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span>
<span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">tup</span><span class="p">):</span>
    <span class="c1"># type: (int, Tuple[Tensor, Tensor]) -&gt; Tensor</span>
    <span class="n">t0</span><span class="p">,</span> <span class="n">t1</span> <span class="o">=</span> <span class="n">tup</span>
    <span class="k">return</span> <span class="n">t0</span> <span class="o">+</span> <span class="n">t1</span> <span class="o">+</span> <span class="n">x</span>

<span class="nb">print</span><span class="p">(</span><span class="n">foo</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">))))</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>It is also possible to annotate types with Python 3 type hints from the
<code class="docutils literal notranslate"><span class="pre">typing</span></code> module.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Tuple</span>

<span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span>
<span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">tup</span><span class="p">:</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">torch</span><span class="o">.</span><span class="n">Tensor</span><span class="p">,</span> <span class="n">torch</span><span class="o">.</span><span class="n">Tensor</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="n">torch</span><span class="o">.</span><span class="n">Tensor</span><span class="p">:</span>
    <span class="n">t0</span><span class="p">,</span> <span class="n">t1</span> <span class="o">=</span> <span class="n">tup</span>
    <span class="k">return</span> <span class="n">t0</span> <span class="o">+</span> <span class="n">t1</span> <span class="o">+</span> <span class="n">x</span>

<span class="nb">print</span><span class="p">(</span><span class="n">foo</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">))))</span>
</pre></div>
</div>
</div>
<p>An empty list is assumed to be <code class="docutils literal notranslate"><span class="pre">List[Tensor]</span></code> and empty dicts
<code class="docutils literal notranslate"><span class="pre">Dict[str,</span> <span class="pre">Tensor]</span></code>. To instantiate an empty list or dict of other types,
use <a href="#id15"><span class="problematic" id="id16">`Python 3 type hints`_</span></a>.</p>
<p>Example (type annotations for Python 3):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torch.nn</span> <span class="k">as</span> <span class="nn">nn</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Dict</span><span class="p">,</span> <span class="n">List</span><span class="p">,</span> <span class="n">Tuple</span>

<span class="k">class</span> <span class="nc">EmptyDataStructures</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">EmptyDataStructures</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">:</span> <span class="n">torch</span><span class="o">.</span><span class="n">Tensor</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">Tuple</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">]],</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">int</span><span class="p">]]:</span>
        <span class="c1"># This annotates the list to be a `List[Tuple[int, float]]`</span>
        <span class="n">my_list</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">Tuple</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">]]</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span>
            <span class="n">my_list</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">i</span><span class="p">,</span> <span class="n">x</span><span class="o">.</span><span class="n">item</span><span class="p">()))</span>

        <span class="n">my_dict</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">return</span> <span class="n">my_list</span><span class="p">,</span> <span class="n">my_dict</span>

<span class="n">x</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">EmptyDataStructures</span><span class="p">())</span>
</pre></div>
</div>
</div>
<div class="section" id="optional-type-refinement">
<h3>Optional Type Refinement<a class="headerlink" href="#optional-type-refinement" title="Permalink to this headline">¶</a></h3>
<p>TorchScript will refine the type of a variable of type <code class="docutils literal notranslate"><span class="pre">Optional[T]</span></code> when
a comparison to <code class="docutils literal notranslate"><span class="pre">None</span></code> is made inside the conditional of an if-statement or checked in an <code class="docutils literal notranslate"><span class="pre">assert</span></code>.
The compiler can reason about multiple <code class="docutils literal notranslate"><span class="pre">None</span></code> checks that are combined with
<code class="docutils literal notranslate"><span class="pre">and</span></code>, <code class="docutils literal notranslate"><span class="pre">or</span></code>, and <code class="docutils literal notranslate"><span class="pre">not</span></code>. Refinement will also occur for else blocks of if-statements
that are not explicitly written.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">None</span></code> check must be within the if-statement’s condition; assigning
a <code class="docutils literal notranslate"><span class="pre">None</span></code> check to a variable and using it in the if-statement’s condition will
not refine the types of variables in the check.
Only local variables will be refined, an attribute like <code class="docutils literal notranslate"><span class="pre">self.x</span></code> will not and must assigned to
a local variable to be refined.</p>
<p>Example (refining types on parameters and locals):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torch.nn</span> <span class="k">as</span> <span class="nn">nn</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Optional</span>

<span class="k">class</span> <span class="nc">M</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="n">z</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">z</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">M</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="c1"># If `z` is None, its type cannot be inferred, so it must</span>
        <span class="c1"># be specified (above)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">z</span> <span class="o">=</span> <span class="n">z</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">):</span>
        <span class="c1"># type: (Optional[int], Optional[int], Optional[int]) -&gt; int</span>
        <span class="k">if</span> <span class="n">x</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">x</span> <span class="o">=</span> <span class="mi">1</span>
            <span class="n">x</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>

        <span class="c1"># Refinement for an attribute by assigning it to a local</span>
        <span class="n">z</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">z</span>
        <span class="k">if</span> <span class="n">y</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">z</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">x</span> <span class="o">=</span> <span class="n">y</span> <span class="o">+</span> <span class="n">z</span>

        <span class="c1"># Refinement via an `assert`</span>
        <span class="k">assert</span> <span class="n">z</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span>
        <span class="n">x</span> <span class="o">+=</span> <span class="n">z</span>
        <span class="k">return</span> <span class="n">x</span>

<span class="n">module</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">M</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span>
<span class="n">module</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">M</span><span class="p">(</span><span class="kc">None</span><span class="p">))</span>
</pre></div>
</div>
</div>
<div class="section" id="id2">
<span id="torchscript-classes"></span><span id="torchscript-class"></span><span id="id3"></span><h3>TorchScript Classes<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h3>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>TorchScript class support is experimental. Currently it is best suited
for simple record-like types (think a <code class="docutils literal notranslate"><span class="pre">NamedTuple</span></code> with methods
attached).</p>
</div>
<p>Python classes can be used in TorchScript if they are annotated with <a class="reference internal" href="jit.html#torch.jit.script" title="torch.jit.script"><code class="xref py py-func docutils literal notranslate"><span class="pre">&#64;torch.jit.script</span></code></a>,
similar to how you would declare a TorchScript function:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span>
<span class="k">class</span> <span class="nc">Foo</span><span class="p">:</span>
  <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">x</span>

  <span class="k">def</span> <span class="nf">aug_add_x</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">inc</span><span class="p">):</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">+=</span> <span class="n">inc</span>
</pre></div>
</div>
<p>This subset is restricted:</p>
<ul>
<li><p>All functions must be valid TorchScript functions (including <code class="docutils literal notranslate"><span class="pre">__init__()</span></code>).</p></li>
<li><p>Classes must be new-style classes, as we use <code class="docutils literal notranslate"><span class="pre">__new__()</span></code> to construct them with pybind11.</p></li>
<li><p>TorchScript classes are statically typed. Members can only be declared by assigning to
self in the <code class="docutils literal notranslate"><span class="pre">__init__()</span></code> method.</p>
<blockquote>
<div><p>For example, assigning to <code class="docutils literal notranslate"><span class="pre">self</span></code> outside of the <code class="docutils literal notranslate"><span class="pre">__init__()</span></code> method:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span>
<span class="k">class</span> <span class="nc">Foo</span><span class="p">:</span>
  <span class="k">def</span> <span class="nf">assign_x</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
</pre></div>
</div>
<p>Will result in:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>RuntimeError:
Tried to set nonexistent attribute: x. Did you forget to initialize it in __init__()?:
def assign_x(self):
  self.x = torch.rand(2, 3)
  ~~~~~~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
</pre></div>
</div>
</div></blockquote>
</li>
<li><p>No expressions except method definitions are allowed in the body of the class.</p></li>
<li><p>No support for inheritance or any other polymorphism strategy, except for inheriting
from <code class="docutils literal notranslate"><span class="pre">object</span></code> to specify a new-style class.</p></li>
</ul>
<p>After a class is defined, it can be used in both TorchScript and Python interchangeably
like any other TorchScript type:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Declare a TorchScript class</span>
<span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span>
<span class="k">class</span> <span class="nc">Pair</span><span class="p">:</span>
  <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">first</span><span class="p">,</span> <span class="n">second</span><span class="p">):</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">first</span> <span class="o">=</span> <span class="n">first</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">second</span> <span class="o">=</span> <span class="n">second</span>

<span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span>
<span class="k">def</span> <span class="nf">sum_pair</span><span class="p">(</span><span class="n">p</span><span class="p">):</span>
  <span class="c1"># type: (Pair) -&gt; Tensor</span>
  <span class="k">return</span> <span class="n">p</span><span class="o">.</span><span class="n">first</span> <span class="o">+</span> <span class="n">p</span><span class="o">.</span><span class="n">second</span>

<span class="n">p</span> <span class="o">=</span> <span class="n">Pair</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">sum_pair</span><span class="p">(</span><span class="n">p</span><span class="p">))</span>
</pre></div>
</div>
</div>
<div class="section" id="named-tuples">
<h3>Named Tuples<a class="headerlink" href="#named-tuples" title="Permalink to this headline">¶</a></h3>
<p>Types produced by <a class="reference external" href="https://docs.python.org/3/library/collections.html#collections.namedtuple" title="(in Python v3.8)"><code class="xref py py-func docutils literal notranslate"><span class="pre">collections.namedtuple</span></code></a> can be used in TorchScript.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">collections</span>

<span class="n">Point</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span><span class="s1">&#39;Point&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="s1">&#39;y&#39;</span><span class="p">])</span>

<span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span>
<span class="k">def</span> <span class="nf">total</span><span class="p">(</span><span class="n">point</span><span class="p">):</span>
    <span class="c1"># type: (Point) -&gt; Tensor</span>
    <span class="k">return</span> <span class="n">point</span><span class="o">.</span><span class="n">x</span> <span class="o">+</span> <span class="n">point</span><span class="o">.</span><span class="n">y</span>

<span class="n">p</span> <span class="o">=</span> <span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="n">y</span><span class="o">=</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">total</span><span class="p">(</span><span class="n">p</span><span class="p">))</span>
</pre></div>
</div>
</div>
<div class="section" id="iterables">
<span id="jit-iterables"></span><h3>Iterables<a class="headerlink" href="#iterables" title="Permalink to this headline">¶</a></h3>
<p>Some functions (for example, <a class="reference external" href="https://docs.python.org/3/library/functions.html#zip" title="(in Python v3.8)"><code class="xref any docutils literal notranslate"><span class="pre">zip</span></code></a> and <a class="reference external" href="https://docs.python.org/3/library/functions.html#enumerate" title="(in Python v3.8)"><code class="xref any docutils literal notranslate"><span class="pre">enumerate</span></code></a>) can only operate on iterable types.
Iterable types in TorchScript include <code class="docutils literal notranslate"><span class="pre">Tensor</span></code>s, lists, tuples, dictionaries, strings,
<a class="reference internal" href="nn.html#torch.nn.ModuleList" title="torch.nn.ModuleList"><code class="xref any py py-class docutils literal notranslate"><span class="pre">torch.nn.ModuleList</span></code></a> and <a class="reference internal" href="nn.html#torch.nn.ModuleDict" title="torch.nn.ModuleDict"><code class="xref any py py-class docutils literal notranslate"><span class="pre">torch.nn.ModuleDict</span></code></a>.</p>
</div>
</div>
<div class="section" id="expressions">
<h2><a class="toc-backref" href="#id7">Expressions</a><a class="headerlink" href="#expressions" title="Permalink to this headline">¶</a></h2>
<p>The following Python Expressions are supported.</p>
<div class="section" id="literals">
<h3>Literals<a class="headerlink" href="#literals" title="Permalink to this headline">¶</a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kc">True</span>
<span class="kc">False</span>
<span class="kc">None</span>
<span class="s1">&#39;string literals&#39;</span>
<span class="s2">&quot;string literals&quot;</span>
<span class="mi">3</span>  <span class="c1"># interpreted as int</span>
<span class="mf">3.4</span>  <span class="c1"># interpreted as a float</span>
</pre></div>
</div>
<div class="section" id="list-construction">
<h4>List Construction<a class="headerlink" href="#list-construction" title="Permalink to this headline">¶</a></h4>
<p>An empty list is assumed have type <code class="docutils literal notranslate"><span class="pre">List[Tensor]</span></code>.
The types of other list literals are derived from the type of the members.
See <a class="reference internal" href="#default-types">Default Types</a> for more details.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">]</span>
<span class="p">[]</span>
<span class="p">[</span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">4</span><span class="p">)]</span>
</pre></div>
</div>
</div>
<div class="section" id="tuple-construction">
<h4>Tuple Construction<a class="headerlink" href="#tuple-construction" title="Permalink to this headline">¶</a></h4>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="p">(</span><span class="mi">3</span><span class="p">,)</span>
</pre></div>
</div>
</div>
<div class="section" id="dict-construction">
<h4>Dict Construction<a class="headerlink" href="#dict-construction" title="Permalink to this headline">¶</a></h4>
<p>An empty dict is assumed have type <code class="docutils literal notranslate"><span class="pre">Dict[str,</span> <span class="pre">Tensor]</span></code>.
The types of other dict literals are derived from the type of the members.
See <a class="reference internal" href="#default-types">Default Types</a> for more details.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">{</span><span class="s1">&#39;hello&#39;</span><span class="p">:</span> <span class="mi">3</span><span class="p">}</span>
<span class="p">{}</span>
<span class="p">{</span><span class="s1">&#39;a&#39;</span><span class="p">:</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="s1">&#39;b&#39;</span><span class="p">:</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">4</span><span class="p">)}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="variables">
<h3>Variables<a class="headerlink" href="#variables" title="Permalink to this headline">¶</a></h3>
<p>See <a class="reference internal" href="#variable-resolution">Variable Resolution</a> for how variables are resolved.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">my_variable_name</span>
</pre></div>
</div>
</div>
<div class="section" id="arithmetic-operators">
<h3>Arithmetic Operators<a class="headerlink" href="#arithmetic-operators" title="Permalink to this headline">¶</a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
<span class="n">a</span> <span class="o">-</span> <span class="n">b</span>
<span class="n">a</span> <span class="o">*</span> <span class="n">b</span>
<span class="n">a</span> <span class="o">/</span> <span class="n">b</span>
<span class="n">a</span> <span class="o">^</span> <span class="n">b</span>
<span class="n">a</span> <span class="o">@</span> <span class="n">b</span>
</pre></div>
</div>
</div>
<div class="section" id="comparison-operators">
<h3>Comparison Operators<a class="headerlink" href="#comparison-operators" title="Permalink to this headline">¶</a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="o">==</span> <span class="n">b</span>
<span class="n">a</span> <span class="o">!=</span> <span class="n">b</span>
<span class="n">a</span> <span class="o">&lt;</span> <span class="n">b</span>
<span class="n">a</span> <span class="o">&gt;</span> <span class="n">b</span>
<span class="n">a</span> <span class="o">&lt;=</span> <span class="n">b</span>
<span class="n">a</span> <span class="o">&gt;=</span> <span class="n">b</span>
</pre></div>
</div>
</div>
<div class="section" id="logical-operators">
<h3>Logical Operators<a class="headerlink" href="#logical-operators" title="Permalink to this headline">¶</a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="ow">and</span> <span class="n">b</span>
<span class="n">a</span> <span class="ow">or</span> <span class="n">b</span>
<span class="ow">not</span> <span class="n">b</span>
</pre></div>
</div>
</div>
<div class="section" id="subscripts-and-slicing">
<h3>Subscripts and Slicing<a class="headerlink" href="#subscripts-and-slicing" title="Permalink to this headline">¶</a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">t</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">t</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="n">t</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span>
<span class="n">t</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
<span class="n">t</span><span class="p">[:</span><span class="mi">1</span><span class="p">]</span>
<span class="n">t</span><span class="p">[:]</span>
<span class="n">t</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
<span class="n">t</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span>
<span class="n">t</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="p">:</span><span class="mi">1</span><span class="p">]</span>
<span class="n">t</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">:,</span> <span class="mi">0</span><span class="p">]</span>
<span class="n">t</span><span class="p">[</span><span class="mi">1</span><span class="p">:,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>
<span class="n">t</span><span class="p">[</span><span class="n">i</span><span class="p">:</span><span class="n">j</span><span class="p">,</span> <span class="n">i</span><span class="p">]</span>
</pre></div>
</div>
</div>
<div class="section" id="function-calls">
<h3>Function Calls<a class="headerlink" href="#function-calls" title="Permalink to this headline">¶</a></h3>
<p>Calls to <a href="#id17"><span class="problematic" id="id18">`builtin functions`_</span></a></p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">torch</span><span class="o">.</span><span class="n">int</span><span class="p">)</span>
</pre></div>
</div>
<p>Calls to other script functions:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>

<span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span>
<span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>

<span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span>
<span class="k">def</span> <span class="nf">bar</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">foo</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="method-calls">
<h3>Method Calls<a class="headerlink" href="#method-calls" title="Permalink to this headline">¶</a></h3>
<p>Calls to methods of builtin types like tensor: <code class="docutils literal notranslate"><span class="pre">x.mm(y)</span></code></p>
<p>On modules, methods must be compiled before they can be called. The TorchScript
compiler recursively compiles methods it sees when compiling other methods. By default,
compilation starts on the <code class="docutils literal notranslate"><span class="pre">forward</span></code> method. Any methods called by <code class="docutils literal notranslate"><span class="pre">forward</span></code> will
be compiled, and any methods called by those methods, and so on. To start compilation at
a method other than <code class="docutils literal notranslate"><span class="pre">forward</span></code>, use the <a class="reference internal" href="jit.html#torch.jit.export" title="torch.jit.export"><code class="xref py py-func docutils literal notranslate"><span class="pre">&#64;torch.jit.export</span></code></a> decorator
(<code class="docutils literal notranslate"><span class="pre">forward</span></code> implicitly is marked <code class="docutils literal notranslate"><span class="pre">&#64;torch.jit.export</span></code>).</p>
<p>Calling a submodule directly (e.g. <code class="docutils literal notranslate"><span class="pre">self.resnet(input)</span></code>) is equivalent to
calling its <code class="docutils literal notranslate"><span class="pre">forward</span></code> method (e.g. <code class="docutils literal notranslate"><span class="pre">self.resnet.forward(input)</span></code>).</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torch.nn</span> <span class="k">as</span> <span class="nn">nn</span>
<span class="kn">import</span> <span class="nn">torchvision</span>

<span class="k">class</span> <span class="nc">MyModule</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">MyModule</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="n">means</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">tensor</span><span class="p">([</span><span class="mf">103.939</span><span class="p">,</span> <span class="mf">116.779</span><span class="p">,</span> <span class="mf">123.68</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">means</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">Parameter</span><span class="p">(</span><span class="n">means</span><span class="o">.</span><span class="n">resize_</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
        <span class="n">resnet</span> <span class="o">=</span> <span class="n">torchvision</span><span class="o">.</span><span class="n">models</span><span class="o">.</span><span class="n">resnet18</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">resnet</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">trace</span><span class="p">(</span><span class="n">resnet</span><span class="p">,</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">224</span><span class="p">,</span> <span class="mi">224</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">helper</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">input</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">resnet</span><span class="p">(</span><span class="nb">input</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">means</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">input</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">helper</span><span class="p">(</span><span class="nb">input</span><span class="p">)</span>

    <span class="c1"># Since nothing in the model calls `top_level_method`, the compiler</span>
    <span class="c1"># must be explicitly told to compile this method</span>
    <span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">export</span>
    <span class="k">def</span> <span class="nf">top_level_method</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">input</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">other_helper</span><span class="p">(</span><span class="nb">input</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">other_helper</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">input</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">input</span> <span class="o">+</span> <span class="mi">10</span>

<span class="c1"># `my_script_module` will have the compiled methods `forward`, `helper`,</span>
<span class="c1"># `top_level_method`, and `other_helper`</span>
<span class="n">my_script_module</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">MyModule</span><span class="p">())</span>
</pre></div>
</div>
</div>
<div class="section" id="ternary-expressions">
<h3>Ternary Expressions<a class="headerlink" href="#ternary-expressions" title="Permalink to this headline">¶</a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="k">if</span> <span class="n">x</span> <span class="o">&gt;</span> <span class="n">y</span> <span class="k">else</span> <span class="n">y</span>
</pre></div>
</div>
</div>
<div class="section" id="casts">
<h3>Casts<a class="headerlink" href="#casts" title="Permalink to this headline">¶</a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>float(ten)
int(3.5)
bool(ten)
str(2)``
</pre></div>
</div>
</div>
<div class="section" id="accessing-module-parameters">
<h3>Accessing Module Parameters<a class="headerlink" href="#accessing-module-parameters" title="Permalink to this headline">¶</a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="bp">self</span><span class="o">.</span><span class="n">my_parameter</span>
<span class="bp">self</span><span class="o">.</span><span class="n">my_submodule</span><span class="o">.</span><span class="n">my_parameter</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="statements">
<h2><a class="toc-backref" href="#id8">Statements</a><a class="headerlink" href="#statements" title="Permalink to this headline">¶</a></h2>
<p>TorchScript supports the following types of statements:</p>
<div class="section" id="simple-assignments">
<h3>Simple Assignments<a class="headerlink" href="#simple-assignments" title="Permalink to this headline">¶</a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">b</span>
<span class="n">a</span> <span class="o">+=</span> <span class="n">b</span> <span class="c1"># short-hand for a = a + b, does not operate in-place on a</span>
<span class="n">a</span> <span class="o">-=</span> <span class="n">b</span>
</pre></div>
</div>
</div>
<div class="section" id="pattern-matching-assignments">
<h3>Pattern Matching Assignments<a class="headerlink" href="#pattern-matching-assignments" title="Permalink to this headline">¶</a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">tuple_or_list</span>
<span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="o">*</span><span class="n">c</span> <span class="o">=</span> <span class="n">a_tuple</span>
</pre></div>
</div>
<p>Multiple Assignments</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span> <span class="o">=</span> <span class="n">tup</span>
</pre></div>
</div>
</div>
<div class="section" id="print-statements">
<h3>Print Statements<a class="headerlink" href="#print-statements" title="Permalink to this headline">¶</a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;the result of an add:&quot;</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="if-statements">
<h3>If Statements<a class="headerlink" href="#if-statements" title="Permalink to this headline">¶</a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">a</span> <span class="o">&lt;</span> <span class="mi">4</span><span class="p">:</span>
    <span class="n">r</span> <span class="o">=</span> <span class="o">-</span><span class="n">a</span>
<span class="k">elif</span> <span class="n">a</span> <span class="o">&lt;</span> <span class="mi">3</span><span class="p">:</span>
    <span class="n">r</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="n">a</span>
<span class="k">else</span><span class="p">:</span>
    <span class="n">r</span> <span class="o">=</span> <span class="mi">3</span> <span class="o">*</span> <span class="n">a</span>
</pre></div>
</div>
<p>In addition to bools, floats, ints, and Tensors can be used in a conditional
and will be implicitly casted to a boolean.</p>
</div>
<div class="section" id="while-loops">
<h3>While Loops<a class="headerlink" href="#while-loops" title="Permalink to this headline">¶</a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">while</span> <span class="n">a</span> <span class="o">&lt;</span> <span class="mi">4</span><span class="p">:</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
    <span class="n">a</span> <span class="o">+=</span> <span class="mi">1</span>
</pre></div>
</div>
</div>
<div class="section" id="for-loops-with-range">
<h3>For loops with range<a class="headerlink" href="#for-loops-with-range" title="Permalink to this headline">¶</a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span>
    <span class="n">x</span> <span class="o">*=</span> <span class="n">i</span>
</pre></div>
</div>
</div>
<div class="section" id="for-loops-over-tuples">
<h3>For loops over tuples<a class="headerlink" href="#for-loops-over-tuples" title="Permalink to this headline">¶</a></h3>
<p>These unroll the loop, generating a body for
each member of the tuple. The body must type-check correctly for each member.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">tup</span> <span class="o">=</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">torch</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">4</span><span class="p">))</span>
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">tup</span><span class="p">:</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="for-loops-over-constant-nn-modulelist">
<h3>For loops over constant nn.ModuleList<a class="headerlink" href="#for-loops-over-constant-nn-modulelist" title="Permalink to this headline">¶</a></h3>
<p>To use a <code class="docutils literal notranslate"><span class="pre">nn.ModuleList</span></code> inside a compiled method, it must be marked
constant by adding the name of the attribute to the <code class="docutils literal notranslate"><span class="pre">__constants__</span></code>
list for the type. For loops over a <code class="docutils literal notranslate"><span class="pre">nn.ModuleList</span></code> will unroll the body of the
loop at compile time, with each member of the constant module list.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">SubModule</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">SubModule</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">weight</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Parameter</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">input</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">weight</span> <span class="o">+</span> <span class="nb">input</span>

<span class="k">class</span> <span class="nc">MyModule</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="n">__constants__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;mods&#39;</span><span class="p">]</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">MyModule</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">mods</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">ModuleList</span><span class="p">([</span><span class="n">SubModule</span><span class="p">()</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)])</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">v</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">module</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">mods</span><span class="p">:</span>
            <span class="n">v</span> <span class="o">=</span> <span class="n">module</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">v</span>


<span class="n">m</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">MyModule</span><span class="p">())</span>
</pre></div>
</div>
</div>
<div class="section" id="break-and-continue">
<h3>Break and Continue<a class="headerlink" href="#break-and-continue" title="Permalink to this headline">¶</a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
    <span class="k">continue</span>
    <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
    <span class="k">break</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="return">
<h3>Return<a class="headerlink" href="#return" title="Permalink to this headline">¶</a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">return</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="variable-resolution">
<h2><a class="toc-backref" href="#id9">Variable Resolution</a><a class="headerlink" href="#variable-resolution" title="Permalink to this headline">¶</a></h2>
<p>TorchScript supports a subset of Python’s variable resolution (i.e. scoping)
rules. Local variables behave the same as in Python, except for the restriction
that a variable must have the same type along all paths through a function.
If a variable has a different type on different branches of an if statement, it
is an error to use it after the end of the if statement.</p>
<p>Similarly, a variable is not allowed to be used if it is only <em>defined</em> along some
paths through the function.</p>
<p>Example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span>
<span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">x</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">y</span> <span class="o">=</span> <span class="mi">4</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Traceback (most recent call last):
  ...
RuntimeError: ...

y is not defined in the false branch...
@torch.jit.script...
def foo(x):
    if x &lt; 0:
    ~~~~~~~~~...  &lt;--- HERE
        y = 4
    print(y)
...
</pre></div>
</div>
<p>Non-local variables are resolved to Python values at compile time when the
function is defined. These values are then converted into TorchScript values using
the rules described in <a class="reference internal" href="#use-of-python-values">Use of Python Values</a>.</p>
</div>
<div class="section" id="use-of-python-values">
<h2><a class="toc-backref" href="#id10">Use of Python Values</a><a class="headerlink" href="#use-of-python-values" title="Permalink to this headline">¶</a></h2>
<p>To make writing TorchScript more convenient, we allow script code to refer
to Python values in the surrounding scope. For instance, any time there is a
reference to <code class="docutils literal notranslate"><span class="pre">torch</span></code>, the TorchScript compiler is actually resolving it to the
<code class="docutils literal notranslate"><span class="pre">torch</span></code> Python module when the function is declared.  These Python values are
not a first class part of TorchScript. Instead they are de-sugared at compile-time
into the primitive types that TorchScript supports. This depends
on the dynamic type of the Python valued referenced when compilation occurs.
This section describes the rules that are used when accessing Python values in TorchScript.</p>
<div class="section" id="functions">
<h3>Functions<a class="headerlink" href="#functions" title="Permalink to this headline">¶</a></h3>
<p>TorchScript can call Python functions. This functionality is very useful when
incrementally converting a model to TorchScript. The model can be moved function-by-function
to TorchScript, leaving calls to Python functions in place. This way you can incrementally
check the correctness of the model as you go.</p>
</div>
<div class="section" id="attribute-lookup-on-python-modules">
<h3>Attribute Lookup On Python Modules<a class="headerlink" href="#attribute-lookup-on-python-modules" title="Permalink to this headline">¶</a></h3>
<p>TorchScript can lookup attributes on modules. <a href="#id19"><span class="problematic" id="id20">`Builtin functions`_</span></a> like <code class="docutils literal notranslate"><span class="pre">torch.add</span></code>
are accessed this way. This allows TorchScript to call functions defined in
other modules.</p>
</div>
<div class="section" id="python-defined-constants">
<span id="constant"></span><h3>Python-defined Constants<a class="headerlink" href="#python-defined-constants" title="Permalink to this headline">¶</a></h3>
<p>TorchScript also provides a way to use constants that are defined in Python.
These can be used to hard-code hyper-parameters into the function, or to
define universal constants. There are two ways of specifying that a Python
value should be treated as a constant.</p>
<ol class="arabic simple">
<li><p>Values looked up as attributes of a module are assumed to be constant:</p></li>
</ol>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">torch</span>

<span class="nd">@torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span>
<span class="k">def</span> <span class="nf">fn</span><span class="p">():</span>
    <span class="k">return</span> <span class="n">math</span><span class="o">.</span><span class="n">pi</span>
</pre></div>
</div>
<ol class="arabic simple" start="2">
<li><p>Attributes of a ScriptModule can be marked constant by annotating them with <code class="docutils literal notranslate"><span class="pre">Final[T]</span></code></p></li>
</ol>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torch.nn</span> <span class="k">as</span> <span class="nn">nn</span>

<span class="k">class</span> <span class="nc">Foo</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="c1"># `Final` from the `typing_extensions` module can also be used</span>
    <span class="n">a</span> <span class="p">:</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">Final</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Foo</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="mi">4</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">input</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">a</span> <span class="o">+</span> <span class="nb">input</span>

<span class="n">f</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">Foo</span><span class="p">())</span>
</pre></div>
</div>
<p>Supported constant Python types are</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">int</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">float</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">bool</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">torch.device</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">torch.layout</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">torch.dtype</span></code></p></li>
<li><p>tuples containing supported types</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">torch.nn.ModuleList</span></code> which can be used in a TorchScript for loop</p></li>
</ul>
</div>
<div class="section" id="module-attributes">
<span id="id4"></span><h3>Module Attributes<a class="headerlink" href="#module-attributes" title="Permalink to this headline">¶</a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">torch.nn.Parameter</span></code> wrapper and <code class="docutils literal notranslate"><span class="pre">register_buffer</span></code> can be used to assign
tensors to a module. Other values assigned to a module that is compiled
will be added to the compiled module if their types can be inferred. All <a class="reference internal" href="#types">types</a>
available in TorchScript can be used as module attributes. Tensor attributes are
semantically the same as buffers. The type of empty lists and dictionaries and <code class="docutils literal notranslate"><span class="pre">None</span></code>
values cannot be inferred and must be specified via
<a class="reference external" href="https://www.python.org/dev/peps/pep-0526/#class-and-instance-variable-annotations">PEP 526-style</a> class annotations.
If a type cannot be inferred and is not explicilty annotated, it will not be added as an attribute
to the resulting <code class="xref py py-class docutils literal notranslate"><span class="pre">ScriptModule</span></code>.</p>
<p>Example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">List</span><span class="p">,</span> <span class="n">Dict</span>

<span class="k">class</span> <span class="nc">Foo</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Module</span><span class="p">):</span>
    <span class="c1"># `words` is initialized as an empty list, so its type must be specified</span>
    <span class="n">words</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span>

    <span class="c1"># The type could potentially be inferred if `a_dict` (below) was not</span>
    <span class="c1"># empty, but this annotation ensures `some_dict` will be made into the</span>
    <span class="c1"># proper type</span>
    <span class="n">some_dict</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">int</span><span class="p">]</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">a_dict</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Foo</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">words</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">some_dict</span> <span class="o">=</span> <span class="n">a_dict</span>

        <span class="c1"># `int`s can be inferred</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">my_int</span> <span class="o">=</span> <span class="mi">10</span>

    <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">input</span><span class="p">):</span>
        <span class="c1"># type: (str) -&gt; int</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">words</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">input</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">some_dict</span><span class="p">[</span><span class="nb">input</span><span class="p">]</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">my_int</span>

<span class="n">f</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">jit</span><span class="o">.</span><span class="n">script</span><span class="p">(</span><span class="n">Foo</span><span class="p">({</span><span class="s1">&#39;hi&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">}))</span>
</pre></div>
</div>
</div>
</div>
</div>


             </article>
             
            </div>
            <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="nn.init.html" class="btn btn-neutral float-right" title="torch.nn.init" accesskey="n" rel="next">Next <img src="_static/images/chevron-right-orange.svg" class="next-page"></a>
      
      
        <a href="jit_builtin_functions.html" class="btn btn-neutral" title="TorchScript Builtins" accesskey="p" rel="prev"><img src="_static/images/chevron-right-orange.svg" class="previous-page"> Previous</a>
      
    </div>
  

  

    <hr>

  

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, Torch Contributors.

    </p>
  </div>
    
      <div>
        Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
      </div>
     

</footer>

          </div>
        </div>

        <div class="pytorch-content-right" id="pytorch-content-right">
          <div class="pytorch-right-menu" id="pytorch-right-menu">
            <div class="pytorch-side-scroll" id="pytorch-side-scroll-right">
              <ul>
<li><a class="reference internal" href="#">TorchScript Language Reference</a><ul>
<li><a class="reference internal" href="#supported-type">Types</a><ul>
<li><a class="reference internal" href="#unsupported-typing-constructs">Unsupported Typing Constructs</a></li>
<li><a class="reference internal" href="#default-types">Default Types</a></li>
<li><a class="reference internal" href="#optional-type-refinement">Optional Type Refinement</a></li>
<li><a class="reference internal" href="#id2">TorchScript Classes</a></li>
<li><a class="reference internal" href="#named-tuples">Named Tuples</a></li>
<li><a class="reference internal" href="#iterables">Iterables</a></li>
</ul>
</li>
<li><a class="reference internal" href="#expressions">Expressions</a><ul>
<li><a class="reference internal" href="#literals">Literals</a><ul>
<li><a class="reference internal" href="#list-construction">List Construction</a></li>
<li><a class="reference internal" href="#tuple-construction">Tuple Construction</a></li>
<li><a class="reference internal" href="#dict-construction">Dict Construction</a></li>
</ul>
</li>
<li><a class="reference internal" href="#variables">Variables</a></li>
<li><a class="reference internal" href="#arithmetic-operators">Arithmetic Operators</a></li>
<li><a class="reference internal" href="#comparison-operators">Comparison Operators</a></li>
<li><a class="reference internal" href="#logical-operators">Logical Operators</a></li>
<li><a class="reference internal" href="#subscripts-and-slicing">Subscripts and Slicing</a></li>
<li><a class="reference internal" href="#function-calls">Function Calls</a></li>
<li><a class="reference internal" href="#method-calls">Method Calls</a></li>
<li><a class="reference internal" href="#ternary-expressions">Ternary Expressions</a></li>
<li><a class="reference internal" href="#casts">Casts</a></li>
<li><a class="reference internal" href="#accessing-module-parameters">Accessing Module Parameters</a></li>
</ul>
</li>
<li><a class="reference internal" href="#statements">Statements</a><ul>
<li><a class="reference internal" href="#simple-assignments">Simple Assignments</a></li>
<li><a class="reference internal" href="#pattern-matching-assignments">Pattern Matching Assignments</a></li>
<li><a class="reference internal" href="#print-statements">Print Statements</a></li>
<li><a class="reference internal" href="#if-statements">If Statements</a></li>
<li><a class="reference internal" href="#while-loops">While Loops</a></li>
<li><a class="reference internal" href="#for-loops-with-range">For loops with range</a></li>
<li><a class="reference internal" href="#for-loops-over-tuples">For loops over tuples</a></li>
<li><a class="reference internal" href="#for-loops-over-constant-nn-modulelist">For loops over constant nn.ModuleList</a></li>
<li><a class="reference internal" href="#break-and-continue">Break and Continue</a></li>
<li><a class="reference internal" href="#return">Return</a></li>
</ul>
</li>
<li><a class="reference internal" href="#variable-resolution">Variable Resolution</a></li>
<li><a class="reference internal" href="#use-of-python-values">Use of Python Values</a><ul>
<li><a class="reference internal" href="#functions">Functions</a></li>
<li><a class="reference internal" href="#attribute-lookup-on-python-modules">Attribute Lookup On Python Modules</a></li>
<li><a class="reference internal" href="#python-defined-constants">Python-defined Constants</a></li>
<li><a class="reference internal" href="#module-attributes">Module Attributes</a></li>
</ul>
</li>
</ul>
</li>
</ul>

            </div>
          </div>
        </div>
      </section>
    </div>

  


  

     
       <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
         <script src="_static/jquery.js"></script>
         <script src="_static/underscore.js"></script>
         <script src="_static/doctools.js"></script>
         <script src="_static/language_data.js"></script>
     

  

  <script type="text/javascript" src="_static/js/vendor/popper.min.js"></script>
  <script type="text/javascript" src="_static/js/vendor/bootstrap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/list.js/1.5.0/list.min.js"></script>
  <script type="text/javascript" src="_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>
 
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-90545585-1', 'auto');
  ga('send', 'pageview');

</script>

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-117752657-2"></script>

<script>
  window.dataLayer = window.dataLayer || [];

  function gtag(){dataLayer.push(arguments);}

  gtag('js', new Date());
  gtag('config', 'UA-117752657-2');
</script>

<img height="1" width="1" style="border-style:none;" alt="" src="https://www.googleadservices.com/pagead/conversion/795629140/?label=txkmCPmdtosBENSssfsC&amp;guid=ON&amp;script=0"/>


  <!-- Begin Footer -->

  <div class="container-fluid docs-tutorials-resources" id="docs-tutorials-resources">
    <div class="container">
      <div class="row">
        <div class="col-md-4 text-center">
          <h2>Docs</h2>
          <p>Access comprehensive developer documentation for PyTorch</p>
          <a class="with-right-arrow" href="https://pytorch.org/docs/stable/index.html">View Docs</a>
        </div>

        <div class="col-md-4 text-center">
          <h2>Tutorials</h2>
          <p>Get in-depth tutorials for beginners and advanced developers</p>
          <a class="with-right-arrow" href="https://pytorch.org/tutorials">View Tutorials</a>
        </div>

        <div class="col-md-4 text-center">
          <h2>Resources</h2>
          <p>Find development resources and get your questions answered</p>
          <a class="with-right-arrow" href="https://pytorch.org/resources">View Resources</a>
        </div>
      </div>
    </div>
  </div>

  <footer class="site-footer">
    <div class="container footer-container">
      <div class="footer-logo-wrapper">
        <a href="https://pytorch.org/" class="footer-logo"></a>
      </div>

      <div class="footer-links-wrapper">
        <div class="footer-links-col">
          <ul>
            <li class="list-title"><a href="https://pytorch.org/">PyTorch</a></li>
            <li><a href="https://pytorch.org/get-started">Get Started</a></li>
            <li><a href="https://pytorch.org/features">Features</a></li>
            <li><a href="https://pytorch.org/ecosystem">Ecosystem</a></li>
            <li><a href="https://pytorch.org/blog/">Blog</a></li>
            <li><a href="https://github.com/pytorch/pytorch/blob/master/CONTRIBUTING.md">Contributing</a></li>
          </ul>
        </div>

        <div class="footer-links-col">
          <ul>
            <li class="list-title"><a href="https://pytorch.org/resources">Resources</a></li>
            <li><a href="https://pytorch.org/tutorials">Tutorials</a></li>
            <li><a href="https://pytorch.org/docs/stable/index.html">Docs</a></li>
            <li><a href="https://discuss.pytorch.org" target="_blank">Discuss</a></li>
            <li><a href="https://github.com/pytorch/pytorch/issues" target="_blank">Github Issues</a></li>
            <li><a href="https://pytorch.org/assets/brand-guidelines/PyTorch-Brand-Guidelines.pdf" target="_blank">Brand Guidelines</a></li>
          </ul>
        </div>

        <div class="footer-links-col follow-us-col">
          <ul>
            <li class="list-title">Stay Connected</li>
            <li>
              <div id="mc_embed_signup">
                <form
                  action="https://twitter.us14.list-manage.com/subscribe/post?u=75419c71fe0a935e53dfa4a3f&id=91d0dccd39"
                  method="post"
                  id="mc-embedded-subscribe-form"
                  name="mc-embedded-subscribe-form"
                  class="email-subscribe-form validate"
                  target="_blank"
                  novalidate>
                  <div id="mc_embed_signup_scroll" class="email-subscribe-form-fields-wrapper">
                    <div class="mc-field-group">
                      <label for="mce-EMAIL" style="display:none;">Email Address</label>
                      <input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL" placeholder="Email Address">
                    </div>

                    <div id="mce-responses" class="clear">
                      <div class="response" id="mce-error-response" style="display:none"></div>
                      <div class="response" id="mce-success-response" style="display:none"></div>
                    </div>    <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->

                    <div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_75419c71fe0a935e53dfa4a3f_91d0dccd39" tabindex="-1" value=""></div>

                    <div class="clear">
                      <input type="submit" value="" name="subscribe" id="mc-embedded-subscribe" class="button email-subscribe-button">
                    </div>
                  </div>
                </form>
              </div>

            </li>
          </ul>

          <div class="footer-social-icons">
            <a href="https://www.facebook.com/pytorch" target="_blank" class="facebook"></a>
            <a href="https://twitter.com/pytorch" target="_blank" class="twitter"></a>
            <a href="https://www.youtube.com/pytorch" target="_blank" class="youtube"></a>
          </div>
        </div>
      </div>
    </div>
  </footer>

  <div class="cookie-banner-wrapper">
  <div class="container">
    <p class="gdpr-notice">To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: <a href="https://www.facebook.com/policies/cookies/">Cookies Policy</a>.</p>
    <img class="close-button" src="_static/images/pytorch-x.svg">
  </div>
</div>

  <!-- End Footer -->

  <!-- Begin Mobile Menu -->

  <div class="mobile-main-menu">
    <div class="container-fluid">
      <div class="container">
        <div class="mobile-main-menu-header-container">
          <a class="header-logo" href="https://pytorch.org/" aria-label="PyTorch"></a>
          <a class="main-menu-close-button" href="#" data-behavior="close-mobile-menu"></a>
        </div>
      </div>
    </div>

    <div class="mobile-main-menu-links-container">
      <div class="main-menu">
        <ul>
          <li>
            <a href="https://pytorch.org/get-started">Get Started</a>
          </li>

          <li>
            <a href="https://pytorch.org/features">Features</a>
          </li>

          <li>
            <a href="https://pytorch.org/ecosystem">Ecosystem</a>
          </li>

          <li>
            <a href="https://pytorch.org/mobile">Mobile</a>
          </li>

          <li>
            <a href="https://pytorch.org/hub">PyTorch Hub</a>
          </li>

          <li>
            <a href="https://pytorch.org/blog/">Blog</a>
          </li>

          <li>
            <a href="https://pytorch.org/tutorials">Tutorials</a>
          </li>

          <li class="active">
            <a href="https://pytorch.org/docs/stable/index.html">Docs</a>
          </li>

          <li>
            <a href="https://pytorch.org/resources">Resources</a>
          </li>

          <li>
            <a href="https://github.com/pytorch/pytorch">Github</a>
          </li>
        </ul>
      </div>
    </div>
  </div>

  <!-- End Mobile Menu -->

  <script type="text/javascript" src="_static/js/vendor/anchor.min.js"></script>

  <script type="text/javascript">
    $(document).ready(function() {
      mobileMenu.bind();
      mobileTOC.bind();
      pytorchAnchors.bind();
      sideMenus.bind();
      scrollToAnchor.bind();
      highlightNavigation.bind();
      mainMenuDropdown.bind();
      filterTags.bind();

      // Remove any empty p tags that Sphinx adds
      $("[data-tags='null']").remove();

      // Add class to links that have code blocks, since we cannot create links in code blocks
      $("article.pytorch-article a span.pre").each(function(e) {
        $(this).closest("a").addClass("has-code");
      });
    })
  </script>
</body>
</html>